/**
 * Created by Administrator on 2017/9/21.
 */
//登录
$(function () {
	var regTel = /^(13|14|15|18)[0-9]{9}$/;
	var regPwd = /\w{6,18}/;
	var flagArr = [];
	//1.手机号验证
	$("#register input").eq(0).on("input",function () {
		if (regTel.test($("#register input")[0].value.trim())) {
			$("#register .r_erro").hide();
			flagArr[0] = true;
		} else {
			$("#register .r_erro").show().children("i").removeClass().addClass("erro iconfont icon-jian1").siblings("span").empty().text("请输入正确的手机");
			flagArr[0] = false;
		}
	})
	//2.密码验证
	$("#register input").eq(1).on("input",function () {
		if (regPwd.test($("#register input")[1].value.trim())) {
			$("#register .r_erro").hide();
			flagArr[1] = true;
		} else {
			$("#register .r_erro").show().children("i").removeClass().addClass("erro iconfont icon-jian1").siblings("span").empty().text("请输入有效的密码");
			flagArr[1] = false;
		}
	})
	//3.随机验证码生成验证码
	function getRandomCode(){
		var arr = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"];
		var str = "";
		for(var i = 0;i < 4;i++){
			var index = parseInt(Math.random() * arr.length)
			str += arr[index];
		}
		return str;
	}
	var str = getRandomCode();
	$("#r_code_btn").text(str);
	$("#r_code_btn")[0].onclick = function () {
		var str = getRandomCode();
		$("#r_code_btn").text(str);
	}
	//验证验证码是否正确
	$("#register input").eq(2).on("input",function () {
		if($("#register input")[2].value.trim()==$("#r_code_btn").text()){
			$("#register .r_erro").hide();
			flagArr[2] = true;
		}else {
			$("#register .r_erro").show().children("i").removeClass().addClass("erro iconfont icon-jian1").siblings("span").empty().text("请填写正确的验证码");
			flagArr[2] = false;
		}
	})
	//判断输入的是否全部有效
	$("#register_btn").on("click",function () {
		for(var i=0;i<3;i++){
			if(!flagArr[i]){
				return $("#register .r_erro span").text("注册失败，请输入正确的格式");
			}
		}
		// 注册接口对接
		var rg_form = document.getElementById("register_form");
		ajax({
			type:"post",
			url:"http://121.199.1.149:8888/user/register",
			data:{
				account:rg_form.uname.value,
				passwd:rg_form.upwd.value,
			},
			success:function (res) {
				console.log(res);
				if(res.code == 0){
					alert("注册成功,是否直接跳转到登录页面");
					// window.location.reload();
					$("#register").hide();
					$("#login").show();
				}else if(res.code == -3){
					$("#register .r_erro").show().children("i").removeClass().addClass("erro iconfont icon-jian1").siblings("span").empty().text("您的手机号已在我们网站注册, 如果忘记密码请联系客服.");
				}else{
					alert("注册失败")
				}
			}
			
		})
	})
	//点击跳转到登录页面
	$("#skip_login_btn").on('click',function () {
		$("#register").hide();
		$("#login").show();
	})
})

